<template>
    <div id="app">
        <router-view></router-view>
    </div>
</template>

<script>
    import electron from 'electron';
    import path from 'path';
    import fs from 'fs';

    export default {
        name: 'auto-code',
        mounted(){
            this.$nextTick(() => {
                this.readBase();
                this.writeEjsTemplate();
            });
        },
        methods: {
            readBase() {
                const dataPath = (electron.app || electron.remote.app).getPath('userData');
                const fileUrl = path.join(dataPath, 'base.json');
                console.log(fileUrl);

                if (fs.existsSync(fileUrl)) {
                    let base;

                    try {
                        base = JSON.parse(fs.readFileSync(fileUrl, 'utf-8'));

                        if (base.loginTimes) {
                            this.$store.commit('base/base', this.clone(base));
                            base.loginTimes += 1;
                            base.lastLoginTime = this.$date.format(new Date(), 'yyyy-MM-dd hh:mm:ss');
                            fs.writeFileSync(fileUrl, JSON.stringify(base));
                            return;
                        }
                    } catch (e) {
                        console.log(e);
                    }
                }

                const base = {
                    loginTimes: 1,
                    lastLoginTime: this.$date.format(new Date(), 'yyyy-MM-dd hh:mm:ss'),
                };

                this.$store.commit('base/base', this.clone(base));
                fs.writeFileSync(fileUrl, JSON.stringify(base));
            },
            writeEjsTemplate(){
                const dataPath = (electron.app || electron.remote.app).getPath('userData');

                this.writeFile(dataPath, 'entity');
                this.writeFile(dataPath, 'bean');
                this.writeFile(dataPath, 'convert');
                this.writeFile(dataPath, 'controller');
                this.writeFile(dataPath, 'service');
                this.writeFile(dataPath, 'serviceImpl');
                this.writeFile(dataPath, 'mapper');
                this.writeFile(dataPath, 'mapperXML');
                this.writeFile(dataPath, 'select');
                this.writeFile(dataPath, 'selectList');
                this.writeFile(dataPath, 'selectPage');
                this.writeFile(dataPath, 'selectPageTotal');
                this.writeFile(dataPath, 'selectByKeyword');
                this.writeFile(dataPath, 'selectTotalByKeyword');
                this.writeFile(dataPath, 'insert');
                this.writeFile(dataPath, 'update');
                this.writeFile(dataPath, 'exception');
                this.writeFile(dataPath, 'enums');
            },
            writeFile(dataPath, filename){
                const filePath = path.join(dataPath, `${filename}.ejs`);
                if (process.env.NODE_ENV === 'development' || !fs.existsSync(filePath)) {
                    const {template} = require(`./components/template/${filename}`);
                    fs.writeFileSync(filePath, template);
                }
            }
        }
    }
</script>

<style>
    * {
        margin: 0;
        padding: 0;
        font-family: "Microsoft YaHei", "Ubuntu Mono", serif;
    }

    body {
        min-width: 930px;
        min-height: 500px;
    }

    ::-webkit-scrollbar {
        width: 8px;
        height: 3px;
        border-radius: 3px;
        background-color: transparent;
    }

    /*定义滚动条轨道 内阴影+圆角*/
    ::-webkit-scrollbar-track {
        -webkit-box-shadow: inset 0 0 6px rgba(100, 182, 243, 0.3);
        background-color: #F5F5F5;
        border-radius: 3px;
    }

    /*定义滑块 内阴影+圆角*/
    ::-webkit-scrollbar-thumb {
        border-radius: 3px;
        background-color: #bbb;
    }

    ::-webkit-scrollbar-thumb:hover {
        background-color: #888;
    }
</style>
